package servletAccesso;

import gestioneContesto.GestoreContesto;

import java.io.IOException;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import login.ControllerAccessoRemote;

/**
 * Servlet implementation class LogoutServlet
 */
public class LogoutServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	GestoreContesto contesto;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LogoutServlet() {
       
    	super();
    	contesto = new GestoreContesto();
      
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		try {
			eseguiRichiesta(request, response);
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		try {
			eseguiRichiesta(request, response);
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	private void eseguiRichiesta(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, NamingException {
		
		ControllerAccessoRemote gestore = contesto.getGestore(ControllerAccessoRemote.class, "ControllerAccessoRemote");
		
		String username = request.getParameter("username");
		String user = request.getParameter("user");
		
		if(user.equals("utente")) {
			
			gestore.logoutUtente(username);
			
		}
		
		else {
			
			gestore.logoutAmministratore();
			
		}
		
		response.sendRedirect("Home.html");
		
	}
	
}
